package cn.wolfcode.ssm.web.controller;


import cn.wolfcode.ssm.domain.Employee;
import cn.wolfcode.ssm.domain.Notice;
import cn.wolfcode.ssm.enums.NoticeStatusEnum;
import cn.wolfcode.ssm.qo.NoticeQueryObject;
import cn.wolfcode.ssm.service.INoticeService;
import cn.wolfcode.ssm.qo.QueryObject;
import cn.wolfcode.ssm.qo.JsonResult;
import cn.wolfcode.ssm.util.RequireAnnotation;
import cn.wolfcode.ssm.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;


    @RequestMapping("/list")
    public String list(HttpServletRequest request,Model model, @ModelAttribute("qo") NoticeQueryObject qo){
        PageInfo<Notice> pageInfo = noticeService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("employee", UserContext.getEmployee());
        model.addAttribute("noticeStatusEnums", NoticeStatusEnum.values());
        return "notice/list";
    }

    @RequestMapping("/delete")
    @RequireAnnotation(name = "公告通知删除",expression = "notice:delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            noticeService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequireAnnotation(name = "公告通知新增/编辑",expression = "notice:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Notice notice){
        if (notice.getId() != null) {
            noticeService.update(notice);
        }else {
            noticeService.save(notice);
        }
        return new JsonResult();
    }

    @RequestMapping("/noticeShow")
    public String updateReadStatus(HttpSession session,Model model, Long id){

        noticeService.updateReadStatus(id);
        Notice notice = noticeService.get(id);
        model.addAttribute("notice",notice);
        Employee employee = UserContext.getEmployee();
        int count = noticeService.countNoRead(employee.getId());
        return "notice/show";
    }

    @RequestMapping("/updateReleaseStatus")
    public String updateReleaseStatus(Long id,Boolean releaseStatus){
        noticeService.updateReleaseStatus(id,releaseStatus);
        return "redirect:/notice/list";
    }


}
